﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Common;
using Newtonsoft.Json;
using Basic.Business;
using Basic.VO;

public partial class Basic_Background_AdminChangePassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //强制刷新页面
        Response.Buffer = true;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        Response.AppendHeader("Pragma", "No-Cache");

        //判断用户是否登录
        if (!Tools.IsAdminSessionValid())
            Tools.ShowInfo(StringTable.ErrorSessionTimeOut, "../Background/AdminLogin.aspx", "_self");
        if (!String.IsNullOrEmpty(Request.QueryString["Action"]))
        {
            Result<Object> result = new Result<object>();
            string action = BadChar.ReplaceBadChar(Request.QueryString["Action"]);
            switch (action)
            {
                case "changePassword":
                    string oldPassword = BadChar.ReplaceBadChar(Request.Form["oldPassword"]);
                    string newPassword = BadChar.ReplaceBadChar(Request.Form["newPassword"]);
                    string confirmPassword = BadChar.ReplaceBadChar(Request.Form["confirmPassword"]);
                    if (!confirmPassword.Equals(newPassword))
                    {
                        result.error = true;
                        result.msg = StringTable.ErrorConfirmPassWord;
                    }
                    else
                    {
                        oldPassword = Tools.MD5Encrypt(oldPassword);
                        newPassword = Tools.MD5Encrypt(newPassword);
                        try
                        {
                            Admin admin=(Admin)Session["admin"];
                            AdminAdmin.UpdatePassword(admin.adminId, oldPassword, newPassword);
                            result.error = false;
                            result.msg = StringTable.SuccessChangePassWord;
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                    }
                    break;
                default:
                    result.error = true;
                    result.msg = StringTable.ErrorUnknowAction;
                    break;
            }
            Response.Write(JavaScriptConvert.SerializeObject(result));
            Response.End();
        }
    }
}
